<?php
require 'Model.php';
	class VideoModel extends Model{
		function __Construct(){
			parent::__Construct();
			$this->mod = 'food';
		}
		function getvideoList(){
			$sql = "select * from {$this->mod} join videotype on food.tid = videotype.tid  ";
		    $data = $this->conn->query($sql);
			return $data;
		}
		function add($data){
			if(!empty($_FILES["pic"]["name"])){
  				$pic = $this->uploadfile('pic','./upload/',array('jpg','gif','png'));
  				}
			  else{
			  	$pic='';
			  }
			
			  $sql = "insert into food(id,name,tid,pic,yl,hittimes,zf) values(null,'".$data['name']."','".$data['tid']."','".$pic."','".$data['yl']."','".$data['hittimes']."','".$data['zf']."')";
			  //执行SQL语句
				$result = $this->conn->query($sql);
					return $result;
		}
		function getOneByID($id){
			$sql = "select * from {$this->mod} left join videotype on food.tid= videotype.tid where id=".$id;
		    $result = $this->conn->query($sql);
	      	return $result;
		}

		function update($data){
			$video = array(
					'id'=>$data['id'],
			'name'=>$data['name'],
			'tid'=>$data['tid'],
			'pic'=>$data['pic'],
			'yl'=>$data['yl'],
			'hittimes'=>$date['hittimes'],
			'zf'=>$data['zf'],
			);
			if(!empty($_FILES["pic"]["name"])){
  				$pic = $this->uploadfile('pic','./Upload/',array('jpg','gif','png'));
  				$video['pic'] = $pic;
  				}
			  
			 		  
			 $sets = '';
			  foreach ($video as $key=>$val){
			        $sets.=$key."='".$val."',";
			    }
			    $sets=rtrim($sets,','); //去掉SQL里的最后一个逗号
			   
			    $sql="UPDATE {$this->mod} SET {$sets} where id=".$data['id'];
			  //执行SQL语句
				$result = $this->conn->query($sql);
					return $result;
		}
		function del($id){
			$sql = "delete from {$this->mod} where id=".$id;
			$result = $this->conn->query($sql);
			return $result;
		}
	}
?>